﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MockObjectSet.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Provides functionality to allow making modifications to a set of data objects.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Tests
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data.Objects;
    using System.Linq;

    /// <summary>
    /// Provides functionality to allow making modifications to a set of data objects.
    /// </summary>
    /// <typeparam name="T">The type of an entity object returned from the query.</typeparam>
    public class MockObjectSet<T> : IObjectSet<T> where T : class
    {
        /// <summary>
        /// Containter for the mock object
        /// </summary>
        private readonly IList<T> _container = new List<T>();

        /// <summary>
        /// Gets the type of the element(s) that are returned when the expression tree associated with this instance of System.Linq.IQueryable is executed.
        /// </summary>
        /// <value>A System.Type that represents the type of the element(s) that are returned when the expression tree associated with this object is executed.</value>
        public Type ElementType
        {
            get { return typeof(T); }
        }

        /// <summary>
        /// Gets the expression tree that is associated with the instance of System.Linq.IQueryable.
        /// </summary>
        /// <value>The System.Linq.Expressions.Expression that is associated with this instance of System.Linq.IQueryable.</value>
        public System.Linq.Expressions.Expression Expression
        {
            get { return _container.AsQueryable().Expression; }
        }

        /// <summary>
        /// Gets the query provider that is associated with this data source.
        /// </summary>
        /// <value>The System.Linq.IQueryProvider that is associated with this data source.</value>
        public IQueryProvider Provider
        {
            get { return _container.AsQueryable().Provider; }
        }

        /// <summary>
        /// Notifies the set that an object that represents a new entity must be added to the set.
        /// </summary>
        /// <param name="entity">The new object to add to the set.</param>
        public void AddObject(T entity)
        {
            _container.Add(entity);
        }

        /// <summary>
        /// Notifies the set that an object that represents an existing entity must be added to the set.
        /// </summary>
        /// <param name="entity">The existing object to add to the set.</param>
        public void Attach(T entity)
        {
            _container.Add(entity);
        }

        /// <summary>
        /// Notifies the set that an object that represents an existing entity must be deleted from the set. </summary>
        /// <param name="entity">The existing object to delete from the set.</param>
        public void DeleteObject(T entity)
        {
            _container.Remove(entity);
        }

        /// <summary>
        /// Notifies the set that an object that represents an existing entity must be detached from the set.
        /// </summary>
        /// <param name="entity">The object to detach from the set.</param>
        public void Detach(T entity)
        {
            _container.Remove(entity);
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>An System.Collections.IEnumerator object that can be used to iterate through the collection.</returns>
        public IEnumerator<T> GetEnumerator()
        {
            return _container.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>An System.Collections.IEnumerator object that can be used to iterate through the collection.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return _container.GetEnumerator();
        }
    }
}